#include "ttMsgMap.h"
#include "winProcCallBacks.h"
#include "ttCalcObjMgr.h"
#include "btnManager.h"
#include "timerManager.h"

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int InitWinProcCallBacks();

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
	InitWinProcCallBacks();
	btnManager::Get()->setInstance(hInstance);
	timerManager::Get()->setInstance(hInstance);
	static TCHAR szAppName[] = TEXT ("HelloWin") ;
	HWND   hwnd ;
	MSG    msg ;
	WNDCLASS wndclass ;

	wndclass.style        = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc  = WndProc ;
	wndclass.cbClsExtra   = 0 ;
	wndclass.cbWndExtra   = 0 ;
	wndclass.hInstance    = hInstance ;
	wndclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION) ;
	wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
	wndclass.hbrBackground= (HBRUSH) GetStockObject (BLACK_BRUSH) ;
	wndclass.lpszMenuName = NULL ;
	wndclass.lpszClassName= szAppName ;

	if (!RegisterClass (&wndclass))
	{
		MessageBox (  NULL, TEXT ("This program requires Windows NT!"),
				szAppName, MB_ICONERROR) ;
		return 0 ;
	}
	hwnd = CreateWindow( szAppName,      // window class name
			TEXT ("The Hello Program"),   // window caption
			WS_POPUP,  // window style
			0,// initial x position
			0,// initial y position
			GetSystemMetrics(SM_CXSCREEN),// initial x size
			GetSystemMetrics(SM_CYSCREEN),// initial y size
			NULL,                 // parent window handle
			NULL,            // window menu handle
			hInstance,   // program instance handle
			NULL) ;      // creation parameters

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;

	memset(&msg, 0, sizeof(msg));
    while (GetMessage(&msg, NULL, 0, 0))  
    {  
        TranslateMessage (&msg) ;  
        DispatchMessage (&msg) ;  
    }
	return msg.wParam ;
}

int InitWinProcCallBacks()
{
	ttMsgMap::Get()->setDefFunc(new onDefault);
#define PUSHMSG(name) ttMsgMap::Get()->push_front(new name);
	PUSHMSG(onSize)
	PUSHMSG(onPaint)
	PUSHMSG(onDestroy)
	PUSHMSG(onKeyDown)
	PUSHMSG(onCreate)
	PUSHMSG(onLButtonDown)
	PUSHMSG(onLButtonUp)
	PUSHMSG(onBtnClick)
	PUSHMSG(onTimer)
	PUSHMSG(onMouseMove)
	PUSHMSG(onRButtonUp)
	return 0;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	return ttMsgMap::Get()->run(hwnd, message, wParam, lParam);
}